﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YiSha.Entity.Business;
using YiSha.Entity.OrganizationManage;
using YiSha.Model.Param.Business;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Service.OrganizationManage;
using YiSha.Util.Extension;
using YiSha.Util.Model;

namespace YiSha.Business.Business
{
    public class PersonBLL
    {
        private PersonService personService = new PersonService();
        private PrescriptionService prescriptionService = new PrescriptionService();
        private DepartmentService departmentService = new DepartmentService();
        #region 获取数据
        public async Task<TData<List<PersonEntity>>> GetList(PersonListParam param)
        {
            TData<List<PersonEntity>> obj = new TData<List<PersonEntity>>();
            obj.Data = await personService.GetList(param);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<PersonEntity>>> GetPageList(PersonListParam param, Pagination pagination)
        {
            TData<List<PersonEntity>> obj = new TData<List<PersonEntity>>();
            obj.Data = await personService.GetPageList(param, pagination);
            List<DepartmentEntity> departmentList = await departmentService.GetList(new DepartmentListParam { Ids = obj.Data.Select(p => p.DepartmentId).ParseToStrings<long>() });
            foreach (PersonEntity person in obj.Data)
            {
                person.DepartmentName = departmentList.Where(p => p.Id == person.DepartmentId).Select(p => p.DepartmentName).FirstOrDefault();
            }

            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<PersonEntity>> GetEntity(long id)
        {
            TData<PersonEntity> obj = new TData<PersonEntity>();
            obj.Data = await personService.GetEntity(id);
            obj.Tag = 1;
            return obj;
        }


        public async Task<TData<PersonEntity>> GetEntityByPrescriptionId(long id)
        {

            var model = prescriptionService.GetEntity(id).Result;
            TData<PersonEntity> obj = new TData<PersonEntity>();
            obj.Data = await personService.GetEntity(model.PersonId.Value);
            obj.Tag = 1;
            return obj;
        }


        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(PersonEntity entity)
        {
            TData<string> obj = new TData<string>();

            if (entity.DepartmentId == 0)
            {
                obj.Message = "请选择所属店铺！";
                return obj;
            }

            if (personService.ExistPersonName(entity))
            {
                obj.Message = "存在名称和手机号相同的档案！";
                return obj;
            }
            await personService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await personService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion
    }
}
